<?php
namespace app\common\controller;
use think\Loader;
use think\Controller;
use think\Db;

/**
* 后台公用基础控制器
* @author taigu
* @version 2017-9-22
*/
class IdexBaseController extends Controller {
	
    protected function _initialize() {
        parent::_initialize();
        
        $cut_action = request()->action();
        
        if(!session('info_user')) {
        	
        	if($cut_action == 'cz_pay_code'){
        		$this->assign('action',request()->action());
        	}else if($cut_action == 'glj' || $cut_action == 'friends' || $cut_action == 'share_erweim'){
        		$this->redirect('index/index/regist?fromuid='.input('fromuid'));
        	}else if($cut_action=='hongb03'){
        	}else{
        		//openid登录
        		$type=get_brower_type();//判断是微信还是支付宝打开
		    	$redirect='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		    	if($type==1){
					$auth_code=input("auth_code");
					$url ='https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='.config('alipay_appid').'&scope=auth_base&redirect_uri='.urlencode($redirect);
				}elseif($type==2){
					$auth_code=input("code");
					$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect';
				}else{
					$this->redirect('index/index/login');
				}
				if(!$auth_code){
					Header("Location: $url");
					exit;
				}else{
					if($type==1){//支付宝openid
						$pay_type = 3;
						$field="alipay_id";
						$openid=user_ali_id($auth_code);
						
					}elseif($type==2){//微信openid
						$pay_type = 2;
						$field="openid";
						$openid=user_wx_id($auth_code);
					}
	        	}
	        	$user_info = db('info_user')->where($field,$openid)->find();
	        	if($user_info){
	        		if($user_info['status'] == 1){
	        			session('info_user', $user_info);
	        			$this->redirect($redirect);
	        		}else{
	        			echo '账号已被冻结';exit;
	        		}
	        	}
        	}
        }
        $this->assign('action',request()->action());
    }
	
	//图片上传
    public function upload(){
    	include_once ('././vendor/aliyun-oss-php-sdk/autoload.php');
		$files = $_POST["value"];  
	    $tmp = base64_decode($files);
	    $fn = time()."_".rand(100000, 999999) . ".jpg";
		$fp = '././public/uploads/image/'. $fn;
		$fp1 = '././public/uploads/image/'. $fn;
	    $res=file_put_contents( $fp, $tmp);
		$ossClient = new \OSS\OssClient('LTAIaqGtJ3JDXJvv', 'Q2kTcyepAHX0VOR44OtszVoyOKE8gl', 'oss-cn-shanghai.aliyuncs.com');
		try{
			$res=$ossClient->uploadFile("long3",$fn,$fp1);
			$src='https://long3.oss-cn-shanghai.aliyuncs.com/'.$fn;
			unlink($fp1);
			return json(["code"=>200,'url'=>$src]);
		} catch(OssException $e) {
			return json(["code"=>404,'msg'=>'上传错误']);
		} 
	} 
	
}
